package com.shop.product.server.service.impl;

import com.shop.product.server.dao.ProductCategoryDao;
import com.shop.product.server.entity.ProductCategory;
import com.shop.product.server.entity.ProductInfo;
import com.shop.product.server.service.ProductCategoryService;
import com.shop.product.server.service.ProductInfoService;
import com.shop.product.server.vo.ProductCategoryVo;
import com.shop.product.server.vo.ProductInfoVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: dpengpeng
 * Date: 2018/5/18
 * Time: 22:58
 * Description:
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    private final ProductInfoService productInfoService;

    private final ProductCategoryDao productCategoryDao;

    @Autowired
    public ProductCategoryServiceImpl(ProductCategoryDao productCategoryDao, ProductInfoService productInfoService) {
        this.productCategoryDao = productCategoryDao;
        this.productInfoService = productInfoService;
    }

    @Override
    public List<ProductCategoryVo> list() {
        //查询所有类目
        List<ProductCategory> productCategoryList = productCategoryDao.findAll();
        List<ProductCategoryVo> productCategoryVoList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {

            //根据类目查询所有商品
            List<ProductInfo> productInfoList = productInfoService.findByCategoryType(productCategory.getCategoryType());
            if (!CollectionUtils.isEmpty(productInfoList)) {
                ProductCategoryVo productCategoryVo = new ProductCategoryVo();
                productCategoryVo.setCategoryName(productCategory.getCategoryName());
                productCategoryVo.setCategoryType(productCategory.getCategoryType());

                List<ProductInfoVo> productInfoVoListList = new ArrayList<>();
                for (ProductInfo productInfo : productInfoList) {
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo, productInfoVo);
                    productInfoVoListList.add(productInfoVo);
                }
                productCategoryVo.setProductInfoVoList(productInfoVoListList);
                productCategoryVoList.add(productCategoryVo);
            }
        }
        return productCategoryVoList;
    }
}
